package com.ftw.commons.utils

import android.graphics.drawable.GradientDrawable
import android.view.View
import com.ftw.commons.DialogRadius

/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019/2/16  10:08 PM
 */

object ShapeUtils {
    /**
     * 设置边框颜色
     */
    fun setStrokeColor(view: View, color: Int) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setStroke(1, color)
        view.background = myGrad
    }

    /**
     * 设置边框颜色
     */
    fun setStrokeWidth(view: View, width: Int, color: Int) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setStroke(width, color)
        view.background = myGrad
    }

    /**
     * 设置弧度
     */
    fun setRadius(view: View, radius: Float) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.cornerRadius = radius
        view.background = myGrad
    }

    /**
     * 设置弧度 四个圆角的顺序为左上，右上，右下，左下。如果X_Radius,Y_Radius为0表示还是直角。
     */
    fun setRadius(view: View, radius: DialogRadius) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.cornerRadii = floatArrayOf(
            radius.topLeft,
            radius.topLeft,
            radius.topRight,
            radius.topRight,
            radius.bottomRight,
            radius.bottomRight,
            radius.bottomLeft,
            radius.bottomLeft
        )
        view.background = myGrad
    }

    /**
     * 设置填充区域颜色
     */
    fun setSolidColor(view: View, color: Int) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setColor(color)
        view.background = myGrad
    }

    /**
     * 设置Shape颜色
     */
    fun setShapeColor(view: View, color: Int) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setStroke(1, color)
        myGrad.setColor(color)
        view.background = myGrad
    }

    /**
     * 设置Shape颜色和弧度
     *
     * @param view   控件
     * @param color  Shape颜色
     * @param radius Shape弧度
     */
    fun setSolidColorAndRadius(view: View, color: Int, radius: Float) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setColor(color)
        myGrad.cornerRadius = radius
        view.background = myGrad
    }


    /**
     * 设置Shape颜色和弧度
     *
     * @param view   控件
     * @param color  Shape颜色
     * @param radius Shape弧度
     */
    fun setShapeColorAndRadius(view: View, color: Int, radius: Float) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setStroke(1, color)
        myGrad.setColor(color)
        myGrad.cornerRadius = radius
        view.background = myGrad
    }

    /**
     * 设置Shape颜色和弧度
     *
     * @param view   控件
     * @param color  Shape颜色
     * @param radius Shape弧度
     */
    fun setShapeColorAndRadius(view: View, radius: Float) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.cornerRadius = radius
        view.background = myGrad
    }

    /**
     * 设置Shape颜色和弧度 stroke
     *
     * @param view   控件
     * @param color  Shape颜色
     * @param radius Shape弧度
     */
    fun setShapeColorAndRadiusAndStroke(view: View, stroke: Int, color: Int, radius: Float) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setStroke(1, stroke)
        myGrad.setColor(color)
        myGrad.cornerRadius = radius
        view.background = myGrad
    }


    /**
     * 设置Shape边框颜色和弧度
     *
     * @param view
     * @param color
     * @param radius
     */
    fun setStrokeColorAndRadius(view: View, color: Int, radius: Float) {
        val myGrad = view.background.mutate() as GradientDrawable
        myGrad.setStroke(1, color)
        myGrad.cornerRadius = radius
        view.background = myGrad
    }


    /**
     * * 设置Shape颜色和弧度
     *
     * @param view        控件
     * @param orientation 渐变方向
     * @param startColor  开始颜色
     * @param endColor    结束颜色
     * @param radius      Shape弧度
     */
    fun setShapeColorsAndRadius(
        view: View,
        orientation: GradientDrawable.Orientation,
        startColor: Int,
        endColor: Int,
        radius: Float
    ) {
        val myGrad = view.background.mutate() as GradientDrawable
        val colors = IntArray(2)
        colors[0] = startColor
        colors[1] = endColor
        myGrad.orientation = orientation
        myGrad.colors = colors
        myGrad.setStroke(0, startColor)
        myGrad.cornerRadius = radius
        view.background = myGrad
    }
}